package advanced.network.basic_servlet;

import java.io.IOException;
import java.net.Socket;

public class Dispatcher implements Runnable {
    private Socket client;
    private Request request;
    private Response response;

    @Override
    public void run() {
        Servlet servlet = WebApp.getServletFromUrl(request.getAction());
        //加入了servlet ,解耦了目标代码
        if (servlet == null)
            response.push2Brower(404, "找不到页面");
        else {
            servlet.service(request, response);
        }
        WebApp.release(client);

    }

    public Dispatcher(Socket clien) {
        this.client = clien;
        //获取请求协议
        try {
            request = new Request(client);
        } catch (IOException e) {
            e.printStackTrace();
            response.push2Brower(500, "程序异常");
            WebApp.release(client);
        }
        //返回响应协议
        response = new Response(client);
        String url = request.getUrl();
    }
}
